<template>
  <div class="add-exam-component-box">
    <div class="add-exam-component">
      <!-- 竞赛信息模块 -->
      <div class="exam-base-info-box">
        <!-- 标题 -->
        <div class="exam-base-title">
          <span class="base-title">{{ type === 'edit' ? '编辑竞赛' : '添加竞赛' }}</span>
          <span class="go-back" @click="goBack">返回</span>
        </div>
        <!-- 基本信息 -->
        <div class="exam-base-info">
          <div class="group-box">
            <div class="group-item">
              <div class="item-label required">竞赛名称</div>
              <div>
                <el-input 
                 v-model="formExam.title" 
                 style="width:420px" 
                 placeholder="请填写竞赛名称"></el-input>
              </div>
            </div>
          </div>
          <div class="group-box">
            <div class="group-item">
              <div class="item-label required">竞赛周期</div>
              <div>
                <el-date-picker 
                 v-model="formExam.examDate" 
                 :disabledDate="disabledDate" 
                 type="datetimerange"
                 start-placeholder="竞赛开始时间" 
                 end-placeholder="竞赛结束时间" 
                 value-format="YYYY-MM-DD HH:mm:ss" />
              </div>
            </div>
          </div>
          <div class="group-box">
            <div class="group-item">
              <el-button 
               class="exam-base-info-button" 
               type="primary" 
               plain 
               @click="saveBaseInfo">保 存</el-button>
            </div>
          </div>
        </div>
      </div>
      <!-- 添加竞赛题目 -->
      <div class="exam-select-question-box">
        <el-button class="exam-add-question" :icon="Plus" type="text" @click="addQuestion()">
          添加题目
        </el-button>
        <el-table height="136px" :data="formExam.examQuestionList" class="question-select-list">
          <el-table-column prop="questionId" width="180px" label="题目id" />
          <el-table-column prop="title" :show-overflow-tooltip="true" label="题目标题" />
          <el-table-column prop="difficulty" width="80px" label="题目难度">
            <template #default="{ row }">
              <div v-if="row.difficulty === 1" style="color:#3EC8FF;">简单</div>
              <div v-if="row.difficulty === 2" style="color:#FE7909;">中等</div>
              <div v-if="row.difficulty === 3" style="color:#FD4C40;">困难</div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="80px">
            <template #default="{ row }">
              <el-button circle type="text" @click="deleteExamQuestion(formExam.examId, row.questionId)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 题目配置模块 题目列表勾选加序号 -->
      <div>
        <el-dialog v-model="dialogVisible">
          <div class="exam-list-box">
            <div class="exam-list-title required">选择竞赛题目</div>
            <el-form inline="true">
              <el-form-item label="题目难度">
                <selector v-model="params.difficulty" style="width: 120px;"></selector>
              </el-form-item>
              <el-form-item label="题目名称">
                <el-input v-model="params.title" placeholder="请您输入要搜索的题目标题" />
              </el-form-item>
              <el-form-item>
                <el-button @click="onSearch" plain>搜索</el-button>
                <el-button @click="onReset" plain type="info">重置</el-button>
              </el-form-item>

            </el-form>
            <!-- 题目列表 -->
            <el-table :data="questionList" @select="handleRowSelect">
              <el-table-column type="selection"></el-table-column>
              <el-table-column prop="questionId" label="题目id" />
              <el-table-column prop="title" label="题目标题" />
              <el-table-column prop="difficulty" label="题目难度">
                <template #default="{ row }">
                  <div v-if="row.difficulty === 1" style="color:#3EC8FF;">简单</div>
                  <div v-if="row.difficulty === 2" style="color:#FE7909;">中等</div>
                  <div v-if="row.difficulty === 3" style="color:#FD4C40;">困难</div>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页区域 -->
            <div class="exam-question-list-button">
              <el-pagination background size="small" layout="total, sizes, prev, pager, next, jumper" :total="total"
                v-model:current-page="params.pageNum" v-model:page-size="params.pageSize"
                :page-sizes="[1, 5, 10, 15, 20]" @size-change="handleSizeChange"
                @current-change="handleCurrentChange" />
              <el-button class="question-select-submit" type="primary" plain
                @click="submitSelectQuestion">提 交</el-button>
            </div>
          </div>
        </el-dialog>
      </div>

      <!-- 提交任务区域 -->
      <div class="submit-box absolute">
        <el-button type="info" plain @click="goBack">暂不发布</el-button>
        <el-button type="primary" plain @click="publishExam">发布竞赛</el-button>
      </div>
    </div>
  </div>
</template>
<script setup>
    import Selector from "@/components/QuestionSelector.vue"
    import { addExamQuestionService, addExamService, deleteExamQuestionService, editExamService, getExamDetailService, publishExamService } from '@/api/exam';
    import router from '@/router';
    import { ElMessage } from 'element-plus';
    import { reactive, ref } from 'vue';
    import { useRoute } from 'vue-router';
    import { getQuestionListService } from "@/api/question";


    //查询字符串接收参数，表示用户是"添加竞赛"或"编辑竞赛"
    const type = useRoute().query.type 

    //控制添加题目窗口的显示/隐藏
    const dialogVisible = ref(false)

    //竞赛相关参数
    const formExam = reactive({
        examId: '',
        title: '',
        examDate: []
        // examQuestionList: []
    })

    //题目列表 -> 总条数
    const total = ref(0)

    //添加题目的列表 -> 列表信息
    const questionList = ref([])

    //获取题目列表的请求参数
    const params = reactive({
        difficulty: '',
        title: '',
        pageSize: 10,
        pageNum: 1
    })

    //被选中的题目的id
    const questionIdSet = ref([])



    const deleteExamQuestion = async (examId, questionId) => {
      await deleteExamQuestionService(examId, questionId)
      getExamDetailById(examId)
      ElMessage.success('竞赛题目删除成功')
    }

    const getExamDetailById = async examId => {
        const examDetail = await getExamDetailService(examId)
        //将所有可枚举属性的值从一个或多个源对象复制到目标对象。它会修改目标对象并返回该对象。
        //也就是说，即使forExam中不存在examQuestionList字段，assign方法也会因为examDetail.data中存在
        //该字段而为formExam添加。
        // console.log("assign前", formExam)

        //当某一竞赛没有题目时，后端详情接口将不会返回examQuestionList的字段，造成在这种情况下
        //assign方法前后formExam.examQuestionList的值不变，因此在赋值前将formExam.examQuestionList
        //手动置为空：
        //1. 当竞赛中不包含题目，详情接口不返回题目列表字段，但由于提前置空，因此实际视图也是无数据的
        //2. 当竞赛中包含题目，详情接口会返回题目列表字段，即使提前置空，后续assign也可以分配正确的数据
        formExam.examQuestionList = []

        Object.assign(formExam, examDetail.data)
        // formExam.title = examDetail.data.title
        // formExam.examQuestionList = examDetail.data.examQuestionList
        //TODO 检查是否额外添加了startTime和endTime并与examDate重复，此时的examDate应该为空
        // console.log("assign后，赋值前", formExam)
        formExam.examDate = [examDetail.data.startTime, examDetail.data.endTime]
        //TODO 此处将examDate进行赋值，但是前面已经添加了startTime和endTime，这就重复了
        //考虑是否放弃Object.assign的赋值方式，改为手动赋值，确保不额外添加startTime和endTime
        // console.log("赋值后", formExam)
    }

    const getExamDetail = async () => {
      const examId = useRoute().query.examId
      if (examId) {
        formExam.examId = examId
        getExamDetailById(examId)
      }
    }
    getExamDetail()


    // /exam/add 保存基本信息 
    // /exam/edit 编辑基本信息
    const saveBaseInfo = async () => {
        if (!formExam.title) {
            return ElMessage.error('请填写竞赛名称')
        }
        if (formExam.examDate.length < 1) {
            return ElMessage.error('请填写竞赛起始时间')
        }
        const fd = new FormData()
        // console.log(formExam)
        for (let key in formExam) {
            if (key === 'examDate') {
                fd.append('startTime', formExam.examDate[0]);
                fd.append('endTime', formExam.examDate[1]);
            } else if (key !== 'startTime' && key !== 'endTime') {
                fd.append(key, formExam[key])
            }
        }
        // fd.forEach((value, key) => console.log(key, value))
        //以编辑打开，会传递examId并由getExamDetail方法赋值给formExam，
        //以此区分保存按钮触发后请求的是add还是edit
        if (formExam.examId) {
          await editExamService(fd)
        } else {
          const addRes = await addExamService(fd)
          formExam.examId = addRes.data
        }
        ElMessage.success('竞赛基本信息保存成功')
    }

    //返回上一页
    const goBack = () => router.go(-1)

    /**
     * 题目列表相关方法
     */
    const getQuestionList = async () => {
        const result = await getQuestionListService(params)
        questionList.value = result.rows
        total.value = result.total
    }

    const handleSizeChange = newPageSize => {
      params.pageNum = 1
      getQuestionList()
    }

    const handleCurrentChange = newPageNum => {
      params.pageNum = newPageNum
      getQuestionList()
    }

    const onSearch = () => {
      params.pageNum = 1
      getQuestionList()
    }


    const onReset = () => {
      params.difficulty = ''
      params.title = ''
      params.pageSize = 10
      params.pageNum = 1
      getQuestionList()
    }

    //点击"添加题目"按钮的回调
    const addQuestion = async () => {
        //首先判断用户是否保存了基本信息
        if (formExam.examId === null || formExam.examId === '') {
            ElMessage.error('请先保存竞赛基本信息')
        } else {
            await getQuestionList(params)
            dialogVisible.value = true
        }
    }

    //题目列表复选框被选择后触发的事件，将被选中的
    const handleRowSelect = selection => {
        questionIdSet.value = []
        selection.forEach(element => {
            questionIdSet.value.push(element.questionId)
        })
    }

    const submitSelectQuestion = async () => {
        if (questionIdSet.value && questionIdSet.value.length < 1) {
            return ElMessage.warning('请选择要添加的题目')
        }
        //TODO 响应式数据？
        const examQ = reactive({
            examId: formExam.examId,
            questionIdSet: questionIdSet.value
        })
        await addExamQuestionService(examQ)
        getExamDetailById(examQ.examId)
        dialogVisible.value = false
        ElMessage.success('竞赛题目添加成功')
    }

    const publishExam = async () => {
      if (formExam.examId === null || formExam.examId === '') {
        return ElMessage.error("请先保存竞赛基本信息")
      }
      await publishExamService(formExam.examId)
      ElMessage.success('发布成功')
      // goBack()
      router.push('/oj/layout/exam')
    }

</script>


<style lang="scss" scoped>
.add-exam-component-box {
  height: 100%;
  overflow: hidden;
  position: relative;
}

.exam-list-box {
  background: #fff;
  padding: 20px 24px;

  .question-select-submit {
    margin-left: 0;
    margin-top: 20px;
    width: 100%;
  }

  .exam-list-title {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.85);
    position: relative;
    padding: 15px 20px;
    padding-top: 0;

    &.required::before {
      position: absolute;
      content: '*';
      font-size: 20px;
      color: red;
      left: 10px;
    }
  }
}

.add-exam-component {
  width: 100%;
  background: #fff;
  padding-bottom: 120px;
  overflow-y: auto;
  box-sizing: border-box;
  height: calc(100vh - 50px);
  margin-top: -10px;

  .exam-select-question-box {

    background: #fff;
    border-bottom: 1px solid #fff;
    border-radius: 2px;
    width: 100%;

    .exam-add-question {
      font-size: 14px;
      float: right;
      margin: 10px 20px 5px 0;
    }

    .question-select-list {
      margin: 0 0 20px 0;
      height: 200px;
    }
  }

  .exam-base-info-box {
    background: #fff;
    border-bottom: 1px solid #fff;
    border-radius: 2px;
    margin-bottom: 10px;
    width: 100%;
    box-sizing: border-box;

    .exam-base-title {
      width: 100%;
      box-sizing: border-box;
      height: 52px;
      border-bottom: 1px solid #e9e9e9;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .base-title {
        font-size: 16px;
        font-weight: 500;
        color: #333333;
      }

      .go-back {
        color: #999;
        cursor: pointer;
      }
    }

    .exam-base-info {
      box-sizing: border-box;
      border-bottom: 1px solid #e9e9e9;
    }

    .mesage-list-content {
      box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.1);
      background-color: rgba(255, 255, 255, 1);
      border-radius: 10px;
      width: 1200px;
      margin-top: 20px;
    }
  }

  .group-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: calc(100% - 64px);
    margin: 24px 0;

    .group-item {
      display: flex;
      align-items: center;
      width: 100%;

      .exam-base-info-button {
        margin-left: 104px;
        width: 420px;
      }

      .item-label {
        font-size: 14px;
        font-weight: 400;
        width: 94px;
        text-align: left;
        color: rgba(0, 0, 0, 0.85);
        position: relative;
        padding-left: 10px;

        &.required::before {
          position: absolute;
          content: '*';
          font-size: 20px;
          color: red;
          left: 0px;
          top: -2px;
        }
      }
    }
  }

  .submit-box {
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;

    &.absolute {
      position: absolute;
      width: calc(100% - 48px);
      bottom: 0;
      background: #fff;
      z-index: 999;
    }
  }
}
</style>

<style>
    .w-e-text-container {
    min-height: 142px;
    }
</style>